﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Diagnostics;
using UIDesigner.Designers.Forms;
using System.Drawing.Design;
using UIDesigner.Designers;
using AFSharedLibrary;
using UIDesigner.DesignContainers.Bases;
using UIDesigner.Editors;
using System.ComponentModel.Design;

namespace UIDesigner.SurfaceProperties
{
    public class SurfaceObjectType : IPropertyCollectionProvider
    {
        private AFDesignerHost m_objHost;
        private PropertyDescriptorCollection m_rgPropertyCollection;
        private IBaseObjectControl m_objControl;
        private SurfacePropertiesBase m_objSurrogateProperties;

        public SurfaceObjectType(AFDesignerHost objHost, IBaseObjectControl objControl)
        {
            m_objHost = objHost;
            m_objControl = objControl;
        }

        public SurfaceObjectType(AFDesignerHost objHost, IBaseObjectControl objControl, SurfacePropertiesBase objSurrogateProperties)
        {
            m_objHost = objHost;
            m_objControl = objControl;
            m_objSurrogateProperties = objSurrogateProperties;
        }

        public override string ToString()
        {
            return m_objControl.Name;
        }

        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public PropertyDescriptorCollection PropertyCollection
        {
            get 
            {
                if (m_rgPropertyCollection == null)
                {
                    if (m_objSurrogateProperties == null)
                    {
                        PropertyDescriptor[] rgProperties = new PropertyDescriptor[9];
                        Attribute[] rgAttr = new Attribute[1];

                        rgAttr[0] = new TypeConverterAttribute(typeof(ExpandableObjectConverter));

                        rgProperties[0] = new SurfaceCategoryDescriptor(m_objHost, m_objControl, SurfacePropertyCategory.ObjectBinding, rgAttr);
                        rgProperties[1] = new SurfaceCategoryDescriptor(m_objHost, m_objControl, SurfacePropertyCategory.ObjectCreation, rgAttr);
                        rgProperties[2] = new SurfaceCategoryDescriptor(m_objHost, m_objControl, SurfacePropertyCategory.ObjectSave, rgAttr);
                        rgProperties[3] = new SurfaceCategoryDescriptor(m_objHost, m_objControl, SurfacePropertyCategory.ObjectDeletion, rgAttr);
                        rgProperties[4] = new SurfaceCategoryDescriptor(m_objHost, m_objControl, SurfacePropertyCategory.UIReset, rgAttr);
                        rgProperties[5] = new SurfaceObjectLevelDescriptor(m_objHost, m_objControl, SurfaceObjectLevelProperty.CancelOptions, null);
                        rgProperties[6] = new SurfaceObjectLevelDescriptor(m_objHost, m_objControl, SurfaceObjectLevelProperty.ApplySelection, null);

                        rgAttr = new Attribute[1];
                        rgAttr[0] = new EditorAttribute(typeof(SurfaceActionEditor), typeof(UITypeEditor));

                        rgProperties[7] = new SurfaceActionDescriptor(m_objHost, m_objControl, SurfaceActionCategory.ApplySavedProperty, rgAttr);
                        rgProperties[8] = new SurfaceActionDescriptor(m_objHost, m_objControl, SurfaceActionCategory.RemoveSurfaceControls, rgAttr);

                        m_rgPropertyCollection = new PropertyDescriptorCollection(rgProperties);
                    }
                    else
                    {
                        PropertyDescriptor[] rgProperties = new PropertyDescriptor[6];
                        Attribute[] rgAttr = new Attribute[1];

                        rgAttr[0] = new TypeConverterAttribute(typeof(ExpandableObjectConverter));

                        rgProperties[0] = new SurfaceCategoryDescriptor(m_objHost, m_objControl, SurfacePropertyCategory.ObjectBinding, rgAttr, m_objSurrogateProperties);
                        rgProperties[1] = new SurfaceCategoryDescriptor(m_objHost, m_objControl, SurfacePropertyCategory.ObjectCreation, rgAttr, m_objSurrogateProperties);
                        rgProperties[2] = new SurfaceCategoryDescriptor(m_objHost, m_objControl, SurfacePropertyCategory.ObjectSave, rgAttr, m_objSurrogateProperties);
                        rgProperties[3] = new SurfaceCategoryDescriptor(m_objHost, m_objControl, SurfacePropertyCategory.ObjectDeletion, rgAttr, m_objSurrogateProperties);
                        rgProperties[4] = new SurfaceCategoryDescriptor(m_objHost, m_objControl, SurfacePropertyCategory.UIReset, rgAttr, m_objSurrogateProperties);
                        rgProperties[5] = new SurfaceObjectLevelDescriptor(m_objHost, m_objControl, SurfaceObjectLevelProperty.CancelOptions, null, m_objSurrogateProperties);

                        m_rgPropertyCollection = new PropertyDescriptorCollection(rgProperties);
                    }
                }

                return m_rgPropertyCollection;
            }
        }
    }

    public class SurfaceObjectDescriptor : PropertyDescriptor
    {
        private AFDesignerHost m_objHost;
        private IBaseObjectControl m_objControl;

        public SurfaceObjectDescriptor(AFDesignerHost objHost, IBaseObjectControl objControl, Attribute[] rgAttributes) : base(objControl.Name, rgAttributes)
		{
            m_objHost = objHost;
            m_objControl = objControl;
		}

        private SurfacePropertiesBase SurfaceProperties
        {
            get
            {
                return m_objHost.SurfacePropertiesSet[m_objControl];
            }
        }

		public override bool CanResetValue(object component)
		{
			return false;
		}

		public override Type ComponentType
		{
			get 
            {
                return m_objHost.RootComponent.GetType(); 
            }
		}

		public override object GetValue(object component)
		{
            SurfaceObjectType objType = new SurfaceObjectType(m_objHost, m_objControl);  

            return objType;
		}

		public override bool IsReadOnly
		{
			get 
            {
                return true; 
            }
		}

		public override Type PropertyType
		{
			get 
            {
                return typeof(SurfaceObjectType); 
            }
		}

		public override void ResetValue(object component)
		{
			
		}

		public override void SetValue(object component, object value)
		{
		}

		public override bool ShouldSerializeValue(object component)
		{
			return false;
		}

		public override string Category
		{
			get
			{
				return "Surface Properties";
			}
		}

		public override string Description
		{
			get
			{
				return "Shows the hidden field names\r\nAlthough these properties exists at the selected object's type you cannot see them as editable";
			}
		}
    }
}
